/*
 * Project it.battlehorse.rcp.tools.log
 * Created on Mar 29, 2006
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */
package it.battlehorse.rcp.tools.log.dialogs;

import it.battlehorse.rcp.sl.annot.Injected;
import it.battlehorse.rcp.sl.annot.Serviceable;
import it.battlehorse.rcp.tools.commons.CommonsActivator;
import it.battlehorse.rcp.tools.log.IDetailHelper;
import it.battlehorse.rcp.tools.log.ILoggable;

import java.util.Date;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;

/**
 * The class which defines the dialog where the log item are shown.
 * 
 * @author battlehorse
 * @since Mar 29, 2006
 */
@Serviceable
public class LogItemDialog extends TitleAreaDialog {
	
	/**
	 * The loggable element which has to be shown
	 */
	private ILoggable detail;
	
	private IDetailHelper helper;
	
	/**
	 * Creates a new instance of the class which will show the given log element
	 * 
	 * @param detail the log element to be shown
	 */
	public LogItemDialog(ILoggable detail) {
		super(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
		this.detail = detail;
	}
	
	/**
	 * Injects an instance of {@code IDetailHelper} into this object
	 * 
	 * @param helper
	 */
	@Injected
	public void setDetailHelper(IDetailHelper helper) {
		this.helper = helper;
	}
	
	@Override protected Control createContents(Composite parent) {
		Control c = super.createContents(parent);
		
		super.setTitle("Log Item details");
		super.setMessage("Detail of the currently selected log item");
		
		return c;
	}
	
	@Override protected void createButtonsForButtonBar(Composite parent) {
		super.createButton(parent,IDialogConstants.OK_ID,IDialogConstants.OK_LABEL, true);
	}
			
	@Override protected Control createDialogArea(Composite parent) {
		
		GridData gd;
		Composite c = new Composite(parent,SWT.NONE);
		c.setLayoutData(new GridData(GridData.FILL_BOTH));
		c.setLayout(new GridLayout(2,false));
		
		
		// Severity
		new Label(c,SWT.NONE).setText("Event severity:");
		
		CLabel cl = new CLabel(c,SWT.NONE);
		cl.setText(detail.getSeverity().getMessage());
		cl.setImage(CommonsActivator.getDefault().getImageRegistry().get(detail.getSeverity().getImageKey()));
		gd = new GridData(GridData.FILL_HORIZONTAL);
		cl.setLayoutData(gd);
		
		// Date
		new Label(c, SWT.NONE).setText("Time:");
		Label l = new Label(c,SWT.NONE);
		l.setText(new Date(detail.getTime()).toString());
		gd = new GridData(GridData.FILL_HORIZONTAL);
		l.setLayoutData(gd);
		
		// Source
		new Label(c, SWT.NONE).setText("Source:");
		l = new Label(c,SWT.NONE);
		l.setText(detail.getSource().toString());
		gd = new GridData(GridData.FILL_HORIZONTAL);
		l.setLayoutData(gd);
		
		// Type
		new Label(c, SWT.NONE).setText("Type:");
		l = new Label(c,SWT.NONE);
		l.setText(detail.getType().getMessage());
		gd = new GridData(GridData.FILL_HORIZONTAL);
		l.setLayoutData(gd);
		
		// Message
		new Label(c, SWT.NONE).setText("Message:");
		l = new Label(c,SWT.NONE);
		l.setText(detail.getMessage());	// TODO : change this one in bold text
		gd = new GridData(GridData.FILL_HORIZONTAL);
		l.setLayoutData(gd);		
		
		// Detail
		if (helper.haveDetails(detail)) {
					
			l = new Label(c,SWT.NONE);
			l.setText("Details:");
			gd = new GridData(GridData.FILL_HORIZONTAL);
			gd.horizontalSpan = 2;
			l.setLayoutData(gd);
			
			helper.addDetailControl(c, detail, 2, SWT.DEFAULT, SWT.DEFAULT);		
		}
										
		return c;
	}
	
	
	
	
}